package com.example.ajouconqueror;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.classes.UserApplication;

public class TeamSelectionAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<String> arrData;
	private LayoutInflater inflater;
	Context gcontext;
	UserApplication UserApp;

	public TeamSelectionAdapter(Context c, ArrayList<String> arr) {
		this.context = c;
		gcontext = c;
		this.arrData = (ArrayList<String>) arr.clone();
		inflater = (LayoutInflater) c
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		UserApp = (UserApplication) context.getApplicationContext();
		try {
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public int getCount() {
		return arrData.size();
	}

	public Object getItem(int position) {
		return arrData.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(
					R.layout.activity_listview_teamselection_item, parent,
					false);
		}
		//set user id
		TextView name = (TextView) convertView.findViewById(R.id.userid);
		name.setText(arrData.get(position));
		//set user;s team number.
		TextView teamint = (TextView) convertView.findViewById(R.id.teamint);
		if (arrData.get(position).equals(UserApp.getTeam1_M1())) {
			teamint.setText("1");
		} else if (arrData.get(position).equals(UserApp.getTeam1_M2())) {
			teamint.setText("1");
		} else if (arrData.get(position).equals(UserApp.getTeam2_M1())) {
			teamint.setText("2");
		} else if (arrData.get(position).equals(UserApp.getTeam2_M2())) {
			teamint.setText("2");
		}
		return convertView;
	}

	public void TeamSRenew(ArrayList<String> REusers) {
		//for renew the data . it is same with other renew logic.
		arrData.clear();
		Log.i("asssssssss", "" + REusers.size());
		for (int i = 0; i < REusers.size(); i++) {
			Log.i("asssssssss", "" + REusers.size());
			Log.i("as", "" + REusers.get(i) + "   " + i);
			String data = REusers.get(i);
			Log.i("as", "" + data);
			arrData.add(data);
		}
		this.notifyDataSetChanged();

		((TeamSelection) context).runOnUiThread(new Runnable() {
			public void run() {
				notifyDataSetChanged();
			}
		});
	}

}
